import os
import xlrd, xlwt

BASE_DIR = os.path.dirname(__file__)


class ExcelOp(object):

    def __init__(self, ex_file):
        self.ex_file = ex_file
        self.ex_data = xlrd.open_workbook(self.ex_file)

    def get_table(self, index=None, sheet_name=None):
        """
        :param index: 表格索引
        :param sheet_name: 表格名字
        :return:
        """
        if sheet_name:
            table = self.ex_data.sheet_by_name(sheet_name)  # 通过sheet名字获取
            return table
        if not index:
            index = 0
        table = self.ex_data.sheet_by_index(index)  # 通过索引获取
        return table

    def excel_run(self):
        table = self.get_table()
        row_values = table.row_values(0)  # 获取0 行的所有数据
        col_values = table.col_values(0)  # 获取0 列的所有数据

        print(row_values)
        print(col_values)


if __name__ == '__main__':
    ex_name = 'test.xlsx'
    ex_file = os.path.join(BASE_DIR, ex_name)
    ex = ExcelOp(ex_file=ex_file)
    ex.excel_run()
